package com.uton.rfidcontroller.view.adapter.scan;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.databinding.library.baseAdapters.BR;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.databinding.ItemScanListBinding;
import com.uton.rfidcontroller.model.CarDealerPatrolDetailModel;
import com.uton.rfidcontroller.view.activity.scan.ScanActivity;
import com.uton.rfidcontroller.view.activity.scan.ScanDetailActivity;

import java.util.List;

/**
 * Created by SEELE on 2017/4/21.
 */

public class OriRVAdapter extends RecyclerView.Adapter<OriRVAdapter.MyHolder> {
    //数据源：String
    private List<CarDealerPatrolDetailModel> dataList;

    private Context mContext;

    public OriRVAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<CarDealerPatrolDetailModel> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemScanListBinding mItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_scan_list, parent, false);

        MyHolder mHolder = new MyHolder(mItemBinding.getRoot());
        mHolder.setBinding(mItemBinding);//把mItemBinding设置给ViewHolder

        return mHolder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        holder.getBinding().setVariable(BR.model, dataList.get(holder.getAdapterPosition()));
        holder.getBinding().rlScanItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ScanActivity) mContext).startActivityForResult(new IntentBuilder()
                        .from(mContext, ScanDetailActivity.class)
                        .setIntParam(GlobalConstants.EXTRA_INT_PARAM1, holder.getAdapterPosition())
                        .setIntParam(GlobalConstants.EXTRA_INT_PARAM2, dataList.get(holder.getAdapterPosition()).getCurrentState())
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, dataList.get(holder.getAdapterPosition()).getRfidLabelId())
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM2,
                                !dataList.get(holder.getAdapterPosition()).isLocked() ? GlobalConstants.UNLOCKED : GlobalConstants.LOCKED)
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM3, dataList.get(holder.getAdapterPosition()).getErrorMsg())
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM4, dataList.get(holder.getAdapterPosition()).getCarId())
                        .setStringParam(GlobalConstants.EXTRA_STRING_PARAM5, dataList.get(holder.getAdapterPosition()).getCarRfidId())
                        .getIntent(), GlobalConstants.REQUEST_WANT_DATA);
            }
        });
        holder.getBinding().executePendingBindings();//立即更新UI
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }


    class MyHolder extends RecyclerView.ViewHolder {
        private ItemScanListBinding binding;

        public MyHolder(View itemView) {
            super(itemView);
        }

        public ItemScanListBinding getBinding() {
            return binding;
        }

        public void setBinding(ItemScanListBinding binding) {
            this.binding = binding;
        }


    }

}
